package com.open.rbac.controller.sys;

import java.util.List;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.open.center.common.model.CommonResult;
import com.open.rbac.config.annotation.TokenUserId;
import com.open.rbac.model.dto.sys.MenuDto;
import com.open.rbac.model.vo.sys.MenuVo;
import com.open.rbac.model.vo.sys.group.MenuUpdate;
import com.open.rbac.service.sys.IMenuService;

/**
 * 菜单控制器
 *
 * @author Riche's
 * @since 2022/8/25
 */

@Slf4j
@Tag(name = "菜单管理", description = "菜单控制器")
@ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功",
                content = {@Content(schema = @Schema(implementation = MenuDto.class))}),
})
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/v1/sys/menu")
public class MenuController {

    private final IMenuService menuService;

    @Operation(summary = "新增菜单")
    @Validated(MenuUpdate.class)
    @PostMapping()
    public CommonResult<MenuDto> create(@Valid @RequestBody MenuVo menuVo) {
        return CommonResult.success(menuService.create(menuVo));
    }

    @Operation(summary = "更新菜单")
    @Validated(MenuUpdate.class)
    @PutMapping()
    public CommonResult<MenuDto> update(@Valid @RequestBody MenuVo menuVo) {
        return CommonResult.success(menuService.update(menuVo));
    }

    @Operation(summary = "按Id查询菜单")
    @GetMapping("/{id}")
    public CommonResult<MenuDto> get(@PathVariable String id) {
        return CommonResult.success(menuService.findById(id));
    }

    @Operation(summary = "删除菜单")
    @DeleteMapping("/{id}")
    public CommonResult<Void> deleteById(@PathVariable String id,
                                         @RequestParam(required = false) Boolean really) {
        if (Boolean.TRUE.equals(really)) {
            menuService.deleteReally(id);
        } else {
            menuService.deleteById(id);
        }
        return CommonResult.success();
    }


    @Operation(summary = "启用|禁用菜单")
    @PutMapping("/enable/{id}")
    public CommonResult<Void> enable(@PathVariable String id,
                                     @RequestParam boolean enable) {
        if (enable) {
            menuService.enable(id);
        } else {
            menuService.disable(id);
        }
        return CommonResult.success();
    }


    @Operation(summary = "按角色Id查询")
    @GetMapping("/by_id")
    public CommonResult<MenuDto> byId(@RequestParam String id) {
        MenuDto menuDto = menuService.findById(id);
        if (menuDto != null && menuDto.getId().equals(id)) {
            return CommonResult.success(null);
        }
        return CommonResult.success(menuDto);
    }

    @Operation(summary = "按菜单名称查询")
    @GetMapping("/by_name")
    public CommonResult<MenuDto> byName(@RequestParam(required = false) String id,
                                        @RequestParam(required = false) String parentId,
                                        @RequestParam String name) {
        parentId = parentId == null ? "0" : parentId;
        List<MenuDto> menuDtos = menuService.listByName(name);
        for (MenuDto menuDto : menuDtos) {
            if (menuDto.getParentId().equals(parentId) && !menuDto.getId().equals(id)) {
                return CommonResult.success(menuDto);
            }
        }
        return CommonResult.success(null);
    }

    @Operation(summary = "查询用户菜单列表")
    @GetMapping("/list_by_user")
    public CommonResult<List<MenuDto>> listByUser(@TokenUserId String userId) {
        return CommonResult.success(menuService.listByUserId(userId));
    }

    @Operation(summary = "查询角色菜单列表")
    @GetMapping("/list_by_role/{roleId}")
    public CommonResult<List<MenuDto>> listByRole(@PathVariable String roleId) {
        return CommonResult.success(menuService.listByRoleId(roleId));
    }

    @Operation(summary = "菜单树")
    @PostMapping("/tree")
    public CommonResult<List<MenuDto>> tree(@RequestBody MenuVo menuVo) {
        return CommonResult.success(menuService.tree(menuVo));
    }

}
